import re

input_path = r'D:\xunji\unity\2.txt'
output_file = r'D:\workspace\study\error_dict_output.txt'

output = []
with open(input_path, 'r', encoding='utf-8') as f:
    for line in f:
        # 跳过无效行
        if not line.strip() or '0X' not in line:
            continue
        # 提取错误码
        code_match = re.search(r'0X\s*([0-9a-fA-F]+)\s*\|', line)
        if not code_match:
            continue
        code =  code_match.group(1).strip()
        # 提取错误信息
        msg_match = re.search(r'chinese discription:(.*?)chinese solution:', line)
        message = msg_match.group(1).strip() if msg_match else ''
        # 提取解决方案
        sol_match = re.search(r'chinese solution:(.*?)english', line)
        solution = sol_match.group(1).strip() if sol_match else ''
        # 生成C#字典项
        output.append(f'    {{"{code}", new ErrorMessageClass{{ code="{code}", message="{message}", solution="{solution}" }} }},')

# 将结果写入新文件
with open(output_file, 'w', encoding='utf-8') as f_out:
   # f_out.write('Dictionary<string, ErrorMessageClass> errorMessageDicts = new Dictionary<string, ErrorMessageClass>\n{\n')
    for line in output:
        f_out.write(line + '\n')
   # f_out.write('};\n')

print(f'已成功写入到: {output_file}')


